<?php

namespace App\Models\Developer;
use App\Http\Common\Email;
use App\Http\Common\MyUtils;
use App\Http\Common\Sms;
use App\Http\Controllers\Auth\RegisterController;
use App\Models\BaseHardNew;
use App\Models\BaseOld;
use App\User;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;

/**
 * 开发者
 * Class Developer
 * @package App\Models
 */
class Developer extends BaseOld
{
    protected $table = "developers";
    public $findAllList = "select d.*,u.username from bf_developers d left join bf_users u on u.id = d.user_id";

    public function myQuery($request)
    {
        parent::myQuery($request);
        $this->sqlSearch($request,'name','like');//通过名称来进行过滤
        $this->sqlSearch($request,'contact_name','like');//通过联系人姓名来进行过滤
        $this->sqlSearch($request,'contact_phone','like');//通过联系人电话来进行过滤
        $this->sqlSearch($request,'contact_email','like');//通过联系人邮箱来进行过滤
    }

    public function store($request)
    {
        return DB::transaction(function ()use($request){
            // 手机验证码验证用户是否正确
            if (!Sms::getInstance()->checkMessageCode($request->code)){
                DB::rollBack();
                return false;
            };
            // 邮箱验证码验证用户是否正确
            if (!Email::getInstance()->checkMessageCode($request->yx_code)){
                DB::rollBack();
                return false;
            };
            MyUtils::setRequest($request,'user_type',7);
            if (!$this->registerUser($request)){
                DB::rollBack();
                return false;
            }
            MyUtils::removeRequest($request,'user_type');
            $res = parent::store($request); // TODO: Change the autogenerated stub
            if ($res){
                DB::commit();
                return $res;
            }else{
                DB::rollBack();
                return false;
            }
        });
    }

    public function storeUpdate($request)
    {
        return DB::transaction(function ()use($request){
            // 手机验证码验证用户是否正确
            if (!Sms::getInstance()->checkMessageCode($request->code)){
                DB::rollBack();
                return false;
            };
            // 邮箱验证码验证用户是否正确
            if (!Email::getInstance()->checkMessageCode($request->yx_code)){
                DB::rollBack();
                return false;
            };
            if (!$this->updateUser($request)){
                DB::rollBack();
                return false;
            }
            $res = parent::storeUpdate($request);
            if ($res){
                DB::commit();
                return $res;
            }else{
                DB::rollBack();
                return false;
            }
        });
    }

    /**
     * 注册用户
     * @param $request
     * @return User|bool
     */
    public function registerUser(& $request){
        $register = new RegisterController();
        $temp = $request->all();
        $temp['group_id'] = 3;// 开发者独有的组id
        $temp['phone'] = $temp['contact_phone'];
        // 注册用户
        $usersId = $register->postPhoneRegister($temp);
        MyUtils::removeRequest($request,'username');
        MyUtils::removeRequest($request,'password');
        MyUtils::removeRequest($request,'code');
        MyUtils::removeRequest($request,'yx_code');
        MyUtils::setRequest($request,'user_id',$usersId);
        return $usersId;
    }

    /**
     * 修改注册用户
     * @param $request
     * @return Factory|View
     */
    public function updateUser(& $request){
        $model = self::find($request->id);
        $register = new RegisterController();
        $temp = $request->all();
        $temp['group_id'] = 3;// 开发者独有的组id
        $temp['phone'] = $temp['contact_phone'];
        $temp['id'] = MyUtils::sg($model,'user_id',0);
        $temp['user_type'] = 7;
        // 注册用户
        $usersId = $register->postPhoneUpdate($temp);
        MyUtils::removeRequest($request,'username');
        MyUtils::removeRequest($request,'password');
        MyUtils::removeRequest($request,'code');
        MyUtils::removeRequest($request,'yx_code');
        return $usersId;
    }

    /**
     * 拥有的套件
     */
    public function packages(){
        return $this->hasMany(DeveloperPackage::class,'developer_id');
    }
    /**
     * 拥有的用户
     */
    public function user(){
        return $this->belongsTo(\App\Models\User::class,'user_id');
    }

    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::deleting(function($models) {
            $packages = $models->packages->all();
            foreach ($packages as $k=>$v){
                $v->delete();
            }
            $user = $models->user;
            if ($user){
                $user->delete();
            }
        });
        static::updating(function($model) {
            if ($model->enable==2){// 为禁用,就同时 packages 禁用
                $packages = $model->packages;
                foreach ($packages as $k=>$v){
                    $v->update(['enable'=>2]);
                }
            }
        });
    }
}
